#include "AuthorityDialog.h"
#include "ui_AuthorityDialog.h"
#include "BootSetting.h"
#include "Mcu.h"

AuthorityDialog *AuthorityDialog::getInstance()
{
    static AuthorityDialog singleton{};
    return &singleton;
}


void AuthorityDialog::changeEvent(QEvent *e) {


    switch (e->type()) {
    case QEvent::LanguageChange:

        ui->retranslateUi(this);


        return;
    default:
        break;
    }
    QDialog::changeEvent(e);

}

void AuthorityDialog::setAuthorityType(AuthorityType type)
{
    g_type = type;
}

AuthorityDialog::AuthorityDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AuthorityDialog)
{
    ui->setupUi(this);
    ui->password->setEchoMode(QLineEdit::Password);
    setWindowModality(Qt::WindowModal);

#ifdef arm
    move((1280 - 500)/2, (800 - 320)/2);
#else
    move((1920 - 500)/2, (1080 - 320)/2);
#endif
}

AuthorityDialog::~AuthorityDialog()
{
    delete ui;
}

void AuthorityDialog::showEvent(QShowEvent *)
{
    ui->password->clear();
}

void AuthorityDialog::on_no_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    hide();
}

void AuthorityDialog::on_yes_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    if (ui->password->text().isEmpty() || ui->password->text().isNull()) {
        ui->info->setText(tr("密码不能为空!"));
        return;
    }
    if (ui->password->text() != "888888") {
        ui->info->setText(tr("密码错误!"));
        return;
    }
    RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId = 0;
    RuntimeData::BootSetting::getInstance()->saveBootSettingInfoToRom();

    hide();
    switch (g_type) {
    case AuthorityType_HomeUi_Export:
        emit HomeUi_Export_Signal();
        break;

    case AuthorityType_SystemSettingUi_ResetFactory:
        emit SystemSettingUi_ResetFactory_Signal();
        break;

    case AuthorityType_SystemSettingUi_UpdateSoftware:
        emit SystemSettingUi_UpdateSoftware_Signal();
        break;

    case AuthorityType_SystemSettingUi_Logoin:
        emit SystemSettingUi_Logoin_Signal();
        break;

    case AuthorityType_PatientInfoUi_Export:
        emit PatientInfoUi_Export_Signal();
        break;

    case AuthorityType_PatientInfoUi_Delete:
        emit PatientInfoUi_Delete_Signal();
        break;

    case AuthorityType_PatientInfoUi_Modify:
        emit PatientInfoUi_Modify_Signal();
        break;

    case AuthorityType_ImageSettingUi_Save:
        emit ImageSettingUi_Save_Signal();
        break;

    case AuthorityType_ImageSettingUi_Recover:
        emit ImageSettingUi_Recover_Signal();
        break;
    }
}
